   package com.javalong.affairs.module.notifything.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.model.TreeElement;

public class TreeCheckViewAdapter extends ArrayAdapter<TreeElement> {
	private int img_leaf = R.drawable.icon_user;// 没有子节点的节点图标
	private int img_expand = R.drawable.outline_list_expand;// 展开的图标
	private int img_collapse = R.drawable.outline_list_collapse;// 收缩的图标
	private int img_tree_space_1 = R.drawable.tree_space_1;// 连接线
	private int img_tree_space_2 = R.drawable.tree_space_2;
	private HashMap<String, String> idList = new HashMap<String, String>();
	private HashMap<String, String> sectionList = new HashMap<String, String>();

	public HashMap<String, String> getIdList() {
		return idList;
	}

	public void setIdList(HashMap<String, String> idList) {
		this.idList = idList;
	}

	private Context context;
	private LayoutInflater mInflater;
	private List<TreeElement> mfilelist;
	private int viewResourceId;

	public TreeCheckViewAdapter(Context context, int viewResourceId,
			List<TreeElement> objects) {
		super(context, viewResourceId, objects);
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.mfilelist = objects;
		this.viewResourceId = viewResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag();
			holder.space.removeAllViews();
		} else {
			convertView = mInflater.inflate(viewResourceId, null);
			holder = new ViewHolder();
			holder.caption = (TextView) convertView.findViewById(R.id.caption);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.space = (LinearLayout) convertView.findViewById(R.id.space);
			holder.cb_men = (CheckBox) convertView.findViewById(R.id.cb_men);
			convertView.setTag(holder);
		}
		final TreeElement treeElement = mfilelist.get(position);
		int level = treeElement.getLevel();
		if (level == 0) {// 根节点

		} else {
			ArrayList<Integer> spaceList = treeElement.getSpaceList();
			// 绘制前面的组织架构线条
			for (int i = 0; i < spaceList.size(); i++) {
				ImageView img = new ImageView(context);
				img.setImageResource(spaceList.get(i));
				holder.space.addView(img);
			}
			ImageView img = new ImageView(context);
			// 节点图标
			if (treeElement.isLastSibling()) {
				img.setImageResource(img_tree_space_2);
			} else {
				img.setImageResource(img_tree_space_1);
			}

			holder.space.addView(img);
		}
		if (treeElement.isHasChild()) {
			if (treeElement.isExpanded()) {
				holder.icon.setImageResource(img_expand);
			} else {
				holder.icon.setImageResource(img_collapse);
			}
		} else {
			holder.icon.setImageResource(img_leaf);
		}
		holder.caption.setText(treeElement.getCaption());// 设置标题
		holder.cb_men.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CheckBox c = (CheckBox) v;
				if (c.isChecked()) {
					addId(treeElement);
				} else {
					removeId(treeElement);
				}
				notifyDataSetChanged();
			}
		});
		if (treeElement.getParent() != null) {
			if (treeElement.getParent().isSelected()) {
				holder.cb_men.setChecked(true);
			} else {
				holder.cb_men.setChecked(treeElement.isSelected());
			}
		} else {
			holder.cb_men.setChecked(treeElement.isSelected());
		}
		return convertView;
	}

	private void addId(TreeElement treeElement) {
		treeElement.setSelected(true);
		doParentSelected(treeElement.getParent());
		if (treeElement.isHasChild()) {
			sectionList.put(treeElement.getId(), treeElement.getId());
			List<TreeElement> trees = treeElement.getChildList();
			for (TreeElement t : trees) {
				addId(t);
			}
		} else {
			if (!idList.containsKey(treeElement.getId())) {
				idList.put(treeElement.getId(), treeElement.getId());
			}
		}
	}

	private void removeId(TreeElement treeElement) {
		treeElement.setSelected(false);
		doParentSelected(treeElement.getParent());
		if (treeElement.isHasChild()) {
			sectionList.remove(treeElement.getId());
			List<TreeElement> trees = treeElement.getChildList();
			for (TreeElement t : trees) {
				removeId(t);
			}
		} else {
			if (idList.containsKey(treeElement.getId())) {
				idList.remove(treeElement.getId());
			}
		}
	}

	private void doParentSelected(TreeElement parent) {
		if (parent == null) {
			return;
		}
		List<TreeElement> list = parent.getChildList();
		if (list == null) {
			return;
		}
		int selectCount = 0;
		for (TreeElement t : list) {
			if (t.isSelected()) {
				selectCount++;
			}
		}
		if (selectCount == list.size()) {
			parent.setSelected(true);
		} else {
			parent.setSelected(false);
		}
		if (parent.getParent() != null) {
			doParentSelected(parent.getParent());
		}
	}

	class ViewHolder {
		LinearLayout space;
		TextView caption;
		ImageView icon;
		CheckBox cb_men;
	}

	public HashMap<String, String> getSectionList() {
		return sectionList;
	}
}
